/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPanelEvento.java
 *
 * Created on 27-11-2011, 06:57:22 PM
 */
package androidutal;

import java.awt.BorderLayout;
import java.io.File;
import java.text.SimpleDateFormat;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Roberto
 */
public class JPanelEvento extends javax.swing.JPanel {
    private final JVentana ventana;
    private final JPanelResumenEvento resumenEvento;
    private File file;
    private boolean isBorrador;
    private boolean ocupado;

    /** Creates new form JPanelEvento */
    public JPanelEvento(JVentana ventana, JPanelResumenEvento resumenEvento) {
        this.ventana = ventana;
        this.file=null;
        this.resumenEvento=resumenEvento;
        initComponents();
        this.setIsBorrador(false);
    }
    public JTextField getjLugar() {
        return jLugar;
        
    }
    public boolean isBorrador() {
        return isBorrador;
    }
    public void setIsBorrador(boolean isBorrador) {
        this.isBorrador = isBorrador;
    }
    public void setjLugar(JTextField jLugar) {
        this.jLugar = jLugar;
    }
    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public JTextField getjAdjunto() {
        return jAdjunto;
    }

    public void setjAdjunto(JTextField jAdjunto) {
        this.jAdjunto = jAdjunto;
    }

    public JComboBox getjHora() {
        return jHora;
    }

    public void setjHora(JComboBox jHora) {
        this.jHora = jHora;
    }

    public JComboBox getjMinuto() {
        return jMinuto;
    }

    public void setjMinuto(JComboBox jMinuto) {
        this.jMinuto = jMinuto;
    }

    public JTextField getjNombre() {
        return jNombre;
    }

    public void setjNombre(JTextField jNombre) {
        this.jNombre = jNombre;
    }

    public JTextArea getjObs() {
        return jObs;
    }

    public void setjObs(JTextArea jObs) {
        this.jObs = jObs;
    }

    public JTextField getjTopico() {
        return jTopico;
    }

    public void setjTopico(JTextField jTopico) {
        this.jTopico = jTopico;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jNombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTopico = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLugar = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jHora = new javax.swing.JComboBox();
        jMinuto = new javax.swing.JComboBox();
        jButton4 = new javax.swing.JButton();
        jAdjunto = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jObs = new javax.swing.JTextArea();

        setBackground(java.awt.Color.black);

        jPanel1.setBackground(java.awt.Color.black);

        jButton2.setBackground(java.awt.Color.white);
        jButton2.setText("Borrador");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setBackground(java.awt.Color.white);
        jButton1.setText("Publicar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setBackground(java.awt.Color.white);
        jButton3.setText("Cancelar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 167, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton3))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton3)
                .addComponent(jButton1)
                .addComponent(jButton2))
        );

        jLabel1.setForeground(java.awt.Color.white);
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Evento");

        jLabel2.setForeground(java.awt.Color.white);
        jLabel2.setText("Nombre:");

        jNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNombreActionPerformed(evt);
            }
        });

        jLabel3.setForeground(java.awt.Color.white);
        jLabel3.setText("Tópico:");

        jTopico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTopicoActionPerformed(evt);
            }
        });

        jLabel4.setBackground(java.awt.Color.white);
        jLabel4.setForeground(java.awt.Color.white);
        jLabel4.setText("Lugar:");

        jLugar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLugarActionPerformed(evt);
            }
        });

        jLabel5.setForeground(java.awt.Color.white);
        jLabel5.setText("Horario:");

        jHora.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3","4","5","6","7","8","9","10", "11", "12", "13","14","15","16","17","18","19","20","21","22","23" }));
        jHora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jHoraActionPerformed(evt);
            }
        });

        jMinuto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "01", "02", "03","04","05","06","07","08","09","10", "11", "12", "13","14","15","16","17","18","19","20", "21", "22", "23","24","25","26","27","28","29","30", "31", "32", "33","34","35","36","37","38","39","40", "41", "42", "43","44","45","46","47","48","49","50", "51", "52", "53","54","55","56","57","58","59"}));

        jButton4.setBackground(java.awt.Color.white);
        jButton4.setText("Adjuntar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jAdjunto.setEditable(false);

        jLabel7.setForeground(java.awt.Color.white);
        jLabel7.setText("Observaciones:");

        jObs.setColumns(20);
        jObs.setLineWrap(true);
        jObs.setRows(5);
        jObs.setWrapStyleWord(true);
        jScrollPane1.setViewportView(jObs);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jHora, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jMinuto, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(243, 243, 243))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jAdjunto, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel7)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLugar, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
                    .addComponent(jTopico, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTopico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLugar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jMinuto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jAdjunto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    String fecha = new SimpleDateFormat("dd/MM/yyyy").format(this.resumenEvento.fechaSeleccionada);
    if(!jNombre.getText().equals("") && !jTopico.getText().equals("") && !jLugar.getText().equals("") && file != null && 
            !jObs.getText().equals("") && this.resumenEvento.getjDateChooser1().getDate() != null){
        this.ventana.getBaseDeDatos().addEvento(jNombre.getText(), jTopico.getText(),
                jLugar.getText(), (String)jHora.getSelectedItem()+":"+(String)jMinuto.getSelectedItem(), file, 
                jObs.getText(),fecha,false,this.ventana.getConfiguracionSubeYBaja().generaUrl(file.getName()));
        this.resumenEvento.insertarInfo((String)jHora.getSelectedItem()+":"+(String)jMinuto.getSelectedItem(), this.jNombre.getText(),"No");
        this.setVisible(false);      
        ventana.add(resumenEvento,BorderLayout.CENTER);
        resumenEvento.setVisible(true);
        ventana.save();
        ventana.saveToWS();
        ventana.subirFTP(file);
    }
    else{
        JOptionPane.showMessageDialog(this, "Rellene todos los campos");
    }
    
    
    //this.resumenEvento.cargarDatos();
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    if(this.isBorrador){
        this.jButton2ActionPerformed(evt);
            if( this.resumenEvento.fechaSeleccionada != null){
                this.setVisible(false);
                ventana.add(resumenEvento,BorderLayout.CENTER);
                this.resumenEvento.cargarDatos();
                resumenEvento.setVisible(true); 
            }
    }
    else{
        this.setVisible(false);
        ventana.add(resumenEvento,BorderLayout.CENTER);
        this.resumenEvento.cargarDatos();
        resumenEvento.setVisible(true); 
    }
    this.desbloquear();
}//GEN-LAST:event_jButton3ActionPerformed

private void jNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNombreActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jNombreActionPerformed

private void jTopicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTopicoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTopicoActionPerformed

private void jLugarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLugarActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jLugarActionPerformed

private void jHoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jHoraActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jHoraActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
     final JFileChooser fc = new JFileChooser();
     
     int result= fc.showOpenDialog(this);
     fc.setVisible(true);
     if (result==JFileChooser.APPROVE_OPTION)
     {
        file = fc.getSelectedFile();
        this.jAdjunto.setText(file.getPath());
     }
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    String fecha = new SimpleDateFormat("dd/MM/yyyy").format(this.resumenEvento.fechaSeleccionada);
            if(file!=null)
            this.ventana.getBaseDeDatos().addEvento(jNombre.getText(), jTopico.getText(),
                    jLugar.getText(), (String)jHora.getSelectedItem()+":"+(String)jMinuto.getSelectedItem(), file, 
                    jObs.getText(),fecha,true,this.ventana.getConfiguracionSubeYBaja().generaUrl(file.getName()));
            else
                this.ventana.getBaseDeDatos().addEvento(jNombre.getText(), jTopico.getText(),
                    jLugar.getText(), (String)jHora.getSelectedItem()+":"+(String)jMinuto.getSelectedItem(), file, 
                    jObs.getText(),fecha,true,"");
            this.resumenEvento.insertarInfo((String)jHora.getSelectedItem()+":"+(String)jMinuto.getSelectedItem(), jNombre.getText(),"Si");
            this.setVisible(false);      
            ventana.add(resumenEvento,BorderLayout.CENTER);
            resumenEvento.setVisible(true);
            ventana.save();
    
    //this.resumenEvento.cargarDatos();
}//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jAdjunto;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jHora;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField jLugar;
    private javax.swing.JComboBox jMinuto;
    private javax.swing.JTextField jNombre;
    private javax.swing.JTextArea jObs;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTopico;
    // End of variables declaration//GEN-END:variables
    public void setHora(String horario) {
        for(int i=0;i<horario.length();i++){
            if(horario.charAt(i)== ':'){
                this.jMinuto.setSelectedItem(horario.substring(i, horario.length()));
                this.jHora.setSelectedItem(horario.substring(0, i));
            }
            
        }
    }
    
    public void bloquear(){
        this.getjNombre().setEditable(false);
        this.getjAdjunto().setEditable(false);
        this.getjLugar().setEditable(false);
        this.getjMinuto().setEnabled(false);
        this.getjHora().setEnabled(false);
        this.getjObs().setEditable(false);
        this.getjTopico().setEditable(false);
        this.jButton1.setVisible(false);
        this.jButton2.setVisible(false);
        this.jButton4.setVisible(false);
        
    }

    public void desbloquear(){
        this.getjNombre().setEditable(true);
        this.getjAdjunto().setEditable(true);
        this.getjLugar().setEditable(true);
        this.getjMinuto().setEnabled(true);
        this.getjHora().setEnabled(true);
        this.getjObs().setEditable(true);
        this.getjTopico().setEditable(true);
        this.jButton1.setVisible(true);
        this.jButton2.setVisible(true);
        this.jButton4.setVisible(true);
    }
}
